#import "ALRatingElementColumnEntry.h"
#import "NSObject+BlockObservation.h"
#import "ALElement.h"
#import "ALElementTemplate.h"


@interface ALRatingElementColumnEntry (ALRatingElementColumnEntryPrivateMethods)
-(void)ratingElementColumnEntryInit_;
-(void)updateHalfStars_;
@end


#pragma mark -
@implementation ALRatingElementColumnEntry

@synthesize valueView;


-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (!(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
		return nil;
	[self ratingElementColumnEntryInit_];
	return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
	if (!(self = [super initWithCoder:aDecoder]))
		return nil;
	[self ratingElementColumnEntryInit_];
	return self;
}

-(void)ratingElementColumnEntryInit_ {
	AMBlockTask halfStarsTask = ^(id object, NSDictionary *change) {
		[self updateHalfStars_];
	};
	[self addObserverForKeyPath:@"representedObject.template.ratingPropertyUsesHalfStars" task:halfStarsTask];
}

-(void)awakeFromNib {
	CGFloat fontSize = [[[self labelView] font] pointSize];
	CGFloat labelViewHeight = [[self labelView] frame].size.height;
	[self setTextPadding:labelViewHeight - fontSize];
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewFrameDidChange:) name:NSViewFrameDidChangeNotification object:[self view]];
}


#pragma mark -
#pragma mark Notifications

-(void)viewFrameDidChange:(NSNotification *)note {
	// This monitors changes in the view's frame due to font size changes (primary elements are larger than secondary elements).  Label and value views are adjusted to fit since text fields won't autoresize vertically
	CGFloat viewHeight = [[self view] frame].size.height;
	
	NSRect labelFrame = [[self labelView] frame];
	labelFrame.size.height = viewHeight;
	[[self labelView] setFrame:labelFrame];
	
	CGFloat labelFontSize = [[[self labelView] font] pointSize];	
	NSRect valueFrame = [[self valueView] frame];
	valueFrame.size.height = labelFontSize;
//	valueFrame.origin.y = (viewHeight / 2) - (labelFontSize / 2);
	[[self valueView] setFrame:valueFrame];
	
}


#pragma mark -
#pragma mark Column entry overrides

-(void)columnEntryDidBeginEditingAtPoint:(NSPoint)clickPoint {
	[[[self valueView] cell] setEditable:YES];
	[super columnEntryDidBeginEditingAtPoint:clickPoint];
}

-(void)columnEntryDidEndEditing {
	[[[self valueView] cell] setEditable:NO];
}

-(NSView *)willForwardHitTestAtPoint:(NSPoint)aPoint toSubview:(NSView *)subview {
	if (subview == [[self valueView] shadowView])
		return [self valueView];
	if (subview == [self valueView] && [[[self valueView] cell] isEditable])
		return subview;
	return [self columnView];
}


#pragma mark -
#pragma mark Private

-(void)updateHalfStars_ {
	if (![[self representedObject] isKindOfClass:[ALElement class]])
		return;
	ALElement *element = (ALElement *)[self representedObject];
	ALElementTemplate *elementTemplate = [element template];
	BOOL allowsHalfStars = [[elementTemplate ratingPropertyUsesHalfStars] boolValue];
	[[self valueView] setAllowsHalfStars:allowsHalfStars];
}

@end
